package cn.zxh.improvement.common.core.function;

import cn.zxh.improvement.common.core.utils.Exceptions;

/**
 * 消费异常包裹
 *
 * @author zxh
 * @since 2023/5/11 11:18:59
 */
@FunctionalInterface
public interface ConsumerWithException<T> extends java.util.function.Consumer<T> {

    /**
     * 消费执行
     *
     * @param arg the input argument
     */
    @Override
    default void accept(T arg) {
        try {
            acceptWithException(arg);
        } catch (Throwable e) {
            throw Exceptions.toRuntime(e);
        }
    }

    /**
     * 执行输入参数的结果，如果发生异常，并抛出异常
     *
     * @param arg 输入参数
     * @throws Throwable 抛出引发的任何异常
     */
    void acceptWithException(T arg) throws Throwable;
}
